﻿/*
 *  RacerLib - XNA Racing Game Level and Tile Management Library
 * 
 *  Copyright (C) 2009 Sebastian Schäfer
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using gdv.VCPR.Racer;

namespace RacerEditorWIN
{
    public partial class EditTileInfo : Form
    {
        TileInfo tileInfo;

        public int Type 
        { 
            get 
            {
                return (int)nudType.Value;
            }
            set
            {
                UpdateCheckBoxes(value);
            }
        }

        public String Filename 
        { 
            get 
            {
                return textBox2.Text;
            }
            set
            {
                textBox2.Text = value;
                try
                {
                    pictureBox1.Image = new Bitmap(value);
                }
                catch
                {
                }
            }
        }

        public String Tilename 
        { 
            get 
            { 
                return textBox1.Text; 
            }
            set
            {
                textBox1.Text = value;
            }
        }

        public TileInfo TileInfo
        {
            get
            {
                return new TileInfo(System.IO.Path.GetFileName(this.Filename), this.Tilename, this.Type, (Bitmap)this.pictureBox1.Image);
            }
            set
            {
                this.Tilename = value.Name;
                this.Filename = value.Filename;
                this.Type = value.Type;
                this.nudType.Value = value.Type;
                this.pictureBox1.Image = value.Bitmap;
            }
        }

        public EditTileInfo()
        {
            InitializeComponent();

            nudType.Minimum = int.MinValue;
            nudType.Maximum = int.MaxValue;

            pictureBox1.Image = TileSet.NoBitmap();
        }

        private void button2_Click(object sender, EventArgs e)
        {
        }

        private void LoadImage(object sender, EventArgs e)
        {
            if (ofDlg.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    pictureBox1.Image = new Bitmap(ofDlg.FileName);
                    textBox2.Text = System.IO.Path.GetFileName(ofDlg.FileName);
                }
                catch
                {
                }
            }
        }

        private void UpdateCheckBoxes(int type)
        {
            Tile t = new Tile();
            t.Type = type;

            checkBox1.Checked = t.HasFlag(ETileTypeFlags.Start);
            checkBox2.Checked = t.HasFlag(ETileTypeFlags.Goal);
            checkBox3.Checked = t.HasFlag(ETileTypeFlags.Checkpoint);
            checkBox4.Checked = t.HasFlag(ETileTypeFlags.Straight);
            checkBox5.Checked = t.HasFlag(ETileTypeFlags.CurveSmall);
            checkBox6.Checked = t.HasFlag(ETileTypeFlags.Flat);
            checkBox7.Checked = t.HasFlag(ETileTypeFlags.Ramp);
            checkBox8.Checked = t.HasFlag(ETileTypeFlags.TiltLeft);
            checkBox9.Checked = t.HasFlag(ETileTypeFlags.TiltedRight);
            checkBox10.Checked = t.HasFlag(ETileTypeFlags.TiltedLeft);
            checkBox11.Checked = t.HasFlag(ETileTypeFlags.TiltRight);
            checkBox12.Checked = t.HasFlag(ETileTypeFlags.Tunneled);
            checkBox13.Checked = t.HasFlag(ETileTypeFlags.Border);
            checkBox14.Checked = t.HasFlag(ETileTypeFlags.Boost);
            checkBox15.Checked = t.HasFlag(ETileTypeFlags.Junction);
        }

        private int RetrieveCheckBoxes()
        {
            Tile t = new Tile();

            //dirty but who in the hell cares...
            t.SetFlag(ETileTypeFlags.Start, checkBox1.Checked);
            t.SetFlag(ETileTypeFlags.Goal, checkBox2.Checked);
            t.SetFlag(ETileTypeFlags.Checkpoint, checkBox3.Checked);
            t.SetFlag(ETileTypeFlags.Straight, checkBox4.Checked);
            t.SetFlag(ETileTypeFlags.CurveSmall, checkBox5.Checked);
            t.SetFlag(ETileTypeFlags.Flat, checkBox6.Checked);
            t.SetFlag(ETileTypeFlags.Ramp, checkBox7.Checked);
            t.SetFlag(ETileTypeFlags.TiltLeft, checkBox8.Checked);
            t.SetFlag(ETileTypeFlags.TiltedRight, checkBox9.Checked);
            t.SetFlag(ETileTypeFlags.TiltedLeft, checkBox10.Checked);
            t.SetFlag(ETileTypeFlags.TiltRight, checkBox11.Checked);
            t.SetFlag(ETileTypeFlags.Tunneled, checkBox12.Checked);
            t.SetFlag(ETileTypeFlags.Border, checkBox13.Checked);
            t.SetFlag(ETileTypeFlags.Boost, checkBox14.Checked);
            t.SetFlag(ETileTypeFlags.Junction, checkBox15.Checked);

            return t.Type;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            nudType.Value = RetrieveCheckBoxes();
        }

        private void nudType_ValueChanged(object sender, EventArgs e)
        {
            UpdateCheckBoxes((int)nudType.Value);
        }
    }
}
